package com.blue.Impl.system;

import com.blue.po.system.FileInfoPO;
import com.blue.service.system.IFileInfoService;
import com.blue.service.system.IFileService;
import com.blue.common.constant.CommonConstant;
import com.blue.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

@Service
public class FileServiceImpl implements IFileService {

    @Value("${file.uploadPath}")
    private String uploadPath;

    @Autowired
    private IFileInfoService fileInfoService;

    @Override
    public String uploadFile(MultipartFile file, String business) throws IOException {
        String fileName = file.getOriginalFilename();
        checkDir();
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        fileName = time + "_" + fileName;
        InputStream inputStream = file.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(uploadPath + fileName);
        FileCopyUtils.copy(inputStream, fileOutputStream);
        String encode = Base64.getEncoder().encodeToString(fileName.getBytes());
        //保存文件信息
        FileInfoPO fileInfoPO = new FileInfoPO();
        fileInfoPO.setBusiness(business);
        fileInfoPO.setCode(encode);
        fileInfoPO.setFileName(fileName);
        fileInfoPO.setPath(uploadPath + fileName);
        fileInfoPO.setType(file.getContentType());
        fileInfoPO.setSize(getFileSize(file.getSize()));
        fileInfoPO.setStatus(CommonConstant.YES);
        fileInfoService.save(fileInfoPO);
        inputStream.close();
        fileOutputStream.close();
        return encode;
    }

    @Override
    public String uploadFile(InputStream inputStream) throws IOException {
        checkDir();
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String fileName = time + ".png";
        File file = new File(uploadPath + fileName);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        FileCopyUtils.copy(inputStream, fileOutputStream);
        String encode = Base64.getEncoder().encodeToString(fileName.getBytes());
        //保存文件信息
        FileInfoPO fileInfoPO = new FileInfoPO();
        fileInfoPO.setBusiness("ai生成图片");
        fileInfoPO.setCode(encode);
        fileInfoPO.setFileName(fileName);
        fileInfoPO.setPath(uploadPath + fileName);
        fileInfoPO.setType("image/png");
        fileInfoPO.setSize(getFileSize(file.length()));
        fileInfoPO.setStatus(CommonConstant.YES);
        fileInfoService.save(fileInfoPO);
        inputStream.close();
        fileOutputStream.close();
        return encode;
    }

    /**
     * 检查文件夹存不存在
     */
    private void checkDir() {
        File uploadPathFile = new File(uploadPath);
        if (!uploadPathFile.exists()) {
            boolean mkDirs = uploadPathFile.mkdirs();
            if (!mkDirs) {
                throw new CustomException("创建文件夹失败");
            }
        }
    }

    /**
     * 获取文件大小
     *
     * @param fileSize
     * @return
     */
    public String getFileSize(long fileSize) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString;
        if (fileSize < 1024) {
            fileSizeString = df.format((double) fileSize) + "B";
        } else if (fileSize < 1048576) {
            fileSizeString = df.format((double) fileSize / 1024) + "KB";
        } else if (fileSize < 1073741824) {
            fileSizeString = df.format((double) fileSize / 1048576) + "MB";
        } else {
            fileSizeString = df.format((double) fileSize / 1073741824) + "GB";
        }
        return fileSizeString;
    }
}
